package com.trkj.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IEplanOrderDao;

@Service
@Transactional
public class OrderPlanService {
	@Autowired
	private IEplanOrderDao eplanOrderDao;
	/**
	 * 生成ID
	 * @return
	 */
	public String createOrderId(){
		String EquipPlanOrderId = "";
		//将当前日期格式化为纯数字
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String str = sdf.format(new Date()).substring(2, 8);
		
		String LikeID = "%" + str + "%";
		List<String> list = eplanOrderDao.queryEquipPlanOrderId(LikeID);

		if(list.isEmpty()){
			EquipPlanOrderId = "EO" + str +"0001";
		}else{
			List<Integer> lists = new ArrayList<>();
			for(int i=0;i<list.size();i++){
				lists.add(Integer.valueOf(list.get(i).toString().substring(8, 12)));
			}
			//Collections.max取最大值再加1，拼接住院号
			 int next = Collections.max(lists).intValue()+1;
			 String nextID = String.format("%04d", next);
			 EquipPlanOrderId = "EO" + str + nextID;
		}
		return EquipPlanOrderId;
	}
}
